<template>
  <div class="serach-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="`${$route.query.keyword} 的搜索结果`"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        @click="$router.push('/article/' + item.art_id)"
        :key="item.art_id"
        :title="item.title"
      />
    </van-list>
    <!-- /文章列表 搜索到的结果-->
  </div>
</template>

<script>
import { getSearch } from '@/api/search.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },

  methods: {
    async onLoad () {
      // 1.发请求获取数据
      // 2.数据要追加到list中
      // 3. loading= false
      // 4.判断是否全部数据加载完成
      const { data: { data } } = await getSearch(this.$route.query.keyword, this.page)
      this.page++
      const arr = data.results
      this.list.push(...arr)
      this.loading = false
      // this.finished = !arr.length
      if (arr.length === 0) this.finished = true
    }
  }
}
</script>

<style lang="less" scoped>
.serach-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
/deep/.van-nav-bar .van-icon {
  color: #fff;
}
</style>
